import { createRouter, createWebHistory } from 'vue-router';
import { reactive } from 'vue';
//路由配置
const routes = reactive([
  {
    path: '/',
    redirect: '/home'
  },
  {
    path: '/home',
    name: 'Home',
    component: () => import("@/pages/Home/index.vue"),
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import("@/pages/login/index.vue"),
  },
  {
    path: '/business',
    name: 'Business',
    component: () => import("@/pages/Business/index.vue"),
    children: [
      {
        path: '',
        redirect: '/business/main'
      },
      {
        path: 'main',
        name: 'Main',
        meta: { title: '首页', icon: 'home' },
        component: () => import('@/pages/Business/components/Main/index.vue'),
        children: [
          {
            path: '',
            redirect: '/business/main/main',
          },
          {
            path: 'main',
            meta: { title: '首页', icon: 'home' },
            component: () => import('@/pages/Business/components/Main/Main/index.vue'),
          },
        ]
      },
      {
        path: 'video',
        name: 'Video',
        meta: { title: '视频监控', icon: 'video' },
        component: () => import('@/pages/Business/components/Video/index.vue'),
        children: [
          {
            path: '',
            redirect: '/business/video/main',
          },
          {
            path: 'main',
            meta: { title: '首页', icon: 'home' },
            component: () => import('@/pages/Business/components/Video/Main/index.vue'),
          },
          {
            path: 'operation1',
            meta: { title: '实时监控', icon: 'monitor' },
            component: () => import('@/pages/Business/components/Video/Operation1/index.vue'),
          },
          {
            path: 'risk-warning',
            meta: { title: '视频风控', icon: 'video' },
            component: () => import('@/pages/Business/components/Video/RiskWarning/index.vue'),
          },
          {
            path: 'photo-center',
            meta: { title: '图片中心', icon: 'pict' },
            component: () => import('@/pages/Business/components/Video/PhotoCenter/index.vue'),
          },
        ]
      },
      {
        path: 'monitor',
        name: 'Monitor',
        meta: { title: '环境监测', icon: 'environment' },
        component: () => import('@/pages/Business/components/Monitor/index.vue'),
        children: [
          {
            path: '',
            redirect: '/business/monitor/main',
          },
          {
            path: 'main',
            meta: { title: '首页', icon: 'home' },
            component: () => import('@/pages/Business/components/Monitor/Main/index.vue'),
          },
          {
            path: 'ev-condition',
            meta: { title: '环境情况', icon: 'environment' },
            component: () => import('@/pages/Business/components/Monitor/Environmental/index.vue'),
          },
        ]
      },
      {
        path: 'control',
        name: 'Control',
        meta: { title: '智能控制', icon: 'control' },
        component: () => import('@/pages/Business/components/Control/index.vue'),
        children: [
          {
            path: '',
            redirect: '/business/control/main',
          },
          {
            path: 'main',
            meta: { title: '首页', icon: 'home' },
            component: () => import('@/pages/Business/components/Control/Main/index.vue'),
          },
          {
            path: 'device',
            meta: { title: '设备控制', icon: 'device' },
            component: () => import('@/pages/Business/components/Control/Device/index.vue'),
          },
          {
            path: 'operation',
            meta: { title: '操作记录', icon: 'history' },
            component: () => import('@/pages/Business/components/Control/Log/index.vue'),
          },
          {
            path: 'statistics',
            meta: { title: '操作统计', icon: 'statistics' },
            component: () => import('@/pages/Business/components/Control/Statistics/index.vue'),
          },
          {
            path: 'ev-dataList',
            meta: { title: '数据列表', icon: 'data' },
            component: () => import('@/pages/Business/components/Control/DataList/index.vue'),
          },
        ]
      },
      {
        path: 'AI',
        name: 'AI',
        meta: { title: '人工智能', icon: 'ai' },
        component: () => import('@/pages/Business/components/AI/index.vue'),
        children: [
          {
            path: '',
            redirect: '/business/AI/Satellite',
          },
          {
            path: 'Satellite',
            meta: { title: '卫星遥感', icon: 'satellite' },
            component: () => import('@/pages/Business/components/AI/Satellite/index.vue'),
          },
          {
            path: 'Patrol',
            meta: { title: '智能巡逻', icon: 'patrol' },
            component: () => import('@/pages/Business/components/AI/Patrol/index.vue'),
          },
          {
            path: 'Prediction',
            meta: { title: '智能预测', icon: 'predict' },
            component: () => import('@/pages/Business/components/AI/Prediction/index.vue'),
          },
          {
            path: 'Knowledge',
            meta: { title: '农业知识图谱', icon: 'knowledgeMap' },
            component: () => import('@/pages/Business/components/AI/Knowledge/index.vue'),
          },
          {
            path: 'Machine',
            meta: { title: '智能机器', icon: 'machine' },
            component: () => import('@/pages/Business/components/AI/Machine/index.vue'),
          },
        ]
      },
      {
        path: 'LLM',
        name: 'LLM',
        meta: { title: 'AI聊天', icon: 'chat' },
        component: () => import('@/pages/Business/components/LLM/index.vue'),
        children: [
          {
            path: '',
            redirect: '/business/LLM/main',
          },
          {
            path: 'main',
            meta: { title: '首页', icon: 'home' },
            component: () => import('@/pages/Business/components/Monitor/Main/index.vue'),
          },
        ]
      },

    ]

  },
  {
    path: '/manage',
    name: 'manage',
    component: () => import('@/pages/Manage/index.vue'),
    children: [
      {
        path: '',
        redirect: '/manage/middleWare'
      },
      {
        path: 'middleWare',
        name: 'middleWareManage',
        meta: { title: '中间件管理', icon: 'middleware' },
        component: () => import('@/pages/Manage/components/middleWare/index.vue')
      },
      {
        path: 'operation',
        name: 'operationManage',
        meta: { title: '操作管理', icon: 'operation' },
        component: () => import('@/pages/Manage/components/operation/index.vue')
      },
      {
        path: 'device',
        name: 'deviceManage',
        meta: { title: '设备管理', icon: 'device' },
        component: () => import('@/pages/Manage/components/device/index.vue')
      },
    ]
  }
]);
// console.log(JSON.stringify(routes[3].children));
// routes[3].children = []

let history=createWebHistory()

const router = createRouter({
  history: history,
  routes
});

export function replaceRoutes(newRoutes) {
  router.matcher = createRouter({
    history: router.history,
    routes: newRoutes
  }).matcher;
}

// 函数用于替换特定父路由下的子路由
export function replaceChildRoutesDirectly(name, newChildRoutes) {
  const parentRoute = routes.find(route => route.name === name);
  if (parentRoute) {
    parentRoute.children = newChildRoutes;
    // 确保更改同步到 matcher 中
    router.matcher = createRouter({
      history: history,
      routes
    }).matcher;
  }
}



export default router;